package cn.bkrc.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 操作缓存中set类型的数据
 */
@Component
public class RedisSetUtils{
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 写入缓存
     * @param k
     * @param vs
     */
    public boolean add(String k,Object... vs){
        boolean result=false;
        try{
            redisTemplate.opsForSet().add(k,vs);
            result=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 读取缓存
     * @param k
     * @return
     */
    public Set<Object> members(String k){
        return redisTemplate.opsForSet().members(k);
    }

    /**
     * 删除缓存中的数据
     * @param k
     * @param objects
     */
    public boolean remove(String k,Object... objects){
        boolean result=false;
        try{
            redisTemplate.opsForSet().remove(k,objects);
            result=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
